﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for leadership
/// </summary>
/// 


namespace Bestlife.ERP.BusinessRules
{
    public class leadership

    {
        String Return_userID;
        Boolean check = false;
        String parent1_status;
        String parent_1;
        String parent2_status;
        String parent3_status;
        String parent4_status;
        String parent5_status;
        String Parent_1;
        String Parent_2;
        String Parent_3;
        String Parent_4;
        String Parent_5;
        public leadership()
        {

            //
            // TODO: Add constructor logic here
            //
        }


        public String calculate_Lbonus(String id, String Status, Double Productpv, Double Productbv)
        {
            // Diamand update by Diamand 
            Bestlife.ERP.DataAccess.D_bonus bns = new DataAccess.D_bonus();//(Add by chathuranga)
            Bestlife.ERP.Entities.E_Bonus ebns1 = new Entities.E_Bonus();//(Add by chathuranga)

            Bestlife.ERP.DataAccess.parents p1 = new Bestlife.ERP.DataAccess.parents();

            #region child month Bv

            Bestlife.ERP.DataAccess.D_bonus bnschild= new DataAccess.D_bonus();//(Add by chathuranga)
            Bestlife.ERP.Entities.E_Bonus ebnschild = new Entities.E_Bonus();//(Add by chathuranga)
            ebnschild = bnschild.Getbonus(id);
            double mothbv = double.Parse(ebnschild.Tbv_month.ToString());

            #endregion

            if (Status.ToString().Equals("7"))
            {
                 Parent_1 = p1.parent(id);
                 DataAccess.D_child ps1 = new DataAccess.D_child();
                 parent1_status = ps1.User_Ststus(Parent_1);

            }
            else if (Status.ToString().Equals("8"))
            {
                 Parent_1 = p1.parent(id);

                 DataAccess.D_child ps1 = new DataAccess.D_child();
                 parent1_status = ps1.User_Ststus(Parent_1);

                 if (Parent_1 != "1")//Super Parent Cannot change.
                 {
                     Parent_2 = p1.parent(Parent_1);

                     //Parent 2 Status
                     DataAccess.D_child ps2 = new DataAccess.D_child();
                     parent2_status = ps2.User_Ststus(Parent_2);
                 }
                 else
                 {
                     Parent_2 = "";
                     parent2_status = "";
                 }
            }
            else if (Status.ToString().Equals("9"))
            {
                Parent_1 = p1.parent(id);
                 DataAccess.D_child ps1 = new DataAccess.D_child();
                 parent1_status = ps1.User_Ststus(Parent_1);

                if (Parent_1 != "1")//Super Parent Cannot change.
                {
                
                        Parent_2 = p1.parent(Parent_1);
                            DataAccess.D_child ps2 = new DataAccess.D_child();
                            parent2_status = ps2.User_Ststus(Parent_2);

                        if (Parent_2 != "1")//Super Parent Cannot change.
                        {
                            Parent_3 = p1.parent(Parent_2);
                            //Parent 2 Status

                            DataAccess.D_child ps3 = new DataAccess.D_child();
                            parent3_status = ps3.User_Ststus(Parent_3);
                        }
                        else 
                        {
                            Parent_3 = "";
                            parent3_status = "";
                        }
                }
                else
                {
                    Parent_2 = "";
                    parent2_status = "";

                    Parent_3 = "";
                    parent3_status = "";
                }

            }
            else if (Status.ToString().Equals("10"))
            {

                Parent_1 = p1.parent(id);
                DataAccess.D_child ps1 = new DataAccess.D_child();
                parent1_status = ps1.User_Ststus(Parent_1);

                if (Parent_1 != "1")//Super Parent Cannot change.
                {

                    Parent_2 = p1.parent(Parent_1);
                    DataAccess.D_child ps2 = new DataAccess.D_child();
                    parent2_status = ps2.User_Ststus(Parent_2);

                    if (Parent_2 != "1")//Super Parent Cannot change.
                    {
                        Parent_3 = p1.parent(Parent_2);
                        //Parent 2 Status

                        DataAccess.D_child ps3 = new DataAccess.D_child();
                        parent3_status = ps3.User_Ststus(Parent_3);


                        if (Parent_3 != "1")//Super Parent Cannot change.
                        {
                            Parent_4 = p1.parent(Parent_3);
                            //Parent 2 Status

                            DataAccess.D_child ps4 = new DataAccess.D_child();
                            parent3_status = ps3.User_Ststus(Parent_4);

                            if (Parent_4 != "1")//Super Parent Cannot change.
                            {
                                Parent_5 = p1.parent(Parent_4);
                                //Parent 2 Status

                                DataAccess.D_child ps5 = new DataAccess.D_child();
                                parent3_status = ps3.User_Ststus(Parent_5);
                            }
                            else
                            {
                                Parent_5 = "";
                                parent5_status = "";
                            }

                        }
                        else
                        {
                            Parent_4 = "";
                            parent4_status = "";
                        }
                    }
                    else
                    {
                        Parent_3 = "";
                        parent3_status = "";
                    }
                }
                else
                {
                    Parent_2 = "";
                    parent2_status = "";

                    Parent_3 = "";
                    parent3_status = "";

                    Parent_4 = "";
                    parent4_status = "";

                    Parent_5 = "";
                    parent5_status = "";

                }


            }



            //only change Final P1 Lcom In 7 Have to change other also
            if (Status.Equals("7") && parent1_status.Equals("7"))
            {
                
                #region Child --> parent -->

                #region Parent1

                ebns1 = bns.Getbonus(Parent_1);
                double T_commitioninDBP1 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P1bonus = ((mothbv * 1) / 100);
                
                Double final_P1_Lcom = P1bonus;//(Add by chathuranga - 25-07-2012)
                // Parent 3 Leadership Bonus
                Boolean LshipCommitionP1 = p1.UpdateLcommition(Parent_1, final_P1_Lcom, Productpv, Productbv);


                #endregion

                #endregion

            }
            //If senior  Diamond
            else if (Status.Equals("8") && parent1_status.Equals("8") && parent2_status.Equals("8"))
            {
                check = true;
                
                #region Child --> parent --> Parent

                #region Parent2

                ebns1 = bns.Getbonus(Parent_2);
                double T_commitioninDBP2 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P2bonus = ((mothbv * 1) / 100);
                Double final_P2_Lcom = T_commitioninDBP2 + P2bonus;
                // Parent 3 Leadership Bonus
                Boolean LshipCommitionP2 = p1.UpdateLcommition(Parent_2, final_P2_Lcom, Productpv, Productbv);

                Return_userID = Parent_2;

                #endregion

                #region Parent1

                ebns1 = bns.Getbonus(Parent_1);
                double T_commitioninDBP1 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P1bonus = ((mothbv * 0.5) / 100);
                Double final_P1_Lcom = T_commitioninDBP1 + P1bonus;
                // Parent 3 Leadership Bonus
                Boolean LshipCommitionP1 = p1.UpdateLcommition(Parent_1, final_P1_Lcom, Productpv, Productbv);

                

                #endregion

                #endregion

            }
            else if (Status.Equals("8") && parent1_status.Equals("8") && !check)
            {
                #region Child --> parent 

                #region Parent1

                ebns1 = bns.Getbonus(Parent_1);
                double T_commitioninDBP1 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P1bonus = ((mothbv * 1) / 100);
                Double final_P1_Lcom = T_commitioninDBP1 + P1bonus;
                // Parent 3 Leadership Bonus
                Boolean LshipCommitionP1 = p1.UpdateLcommition(Parent_1, final_P1_Lcom, Productpv, Productbv);

                Return_userID = parent_1;
                #endregion

                #endregion
            }
            //If senior Executive Diamond

                //------------------------------------------give check state ?? have to insert this check (If (check)=true)// add by chathuranga 25-07-2012
            else if (Status.Equals("9") && parent1_status.Equals("9") && parent2_status.Equals("9") && parent3_status.Equals("9"))
            {
                #region Child --> parent --> Parent --> Parent

                #region Parent3

                ebns1 = bns.Getbonus(Parent_3);
                double T_commitioninDB = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P3bonuss = ((mothbv * 0.5) / 100);
                Double final_P3_Lcom = T_commitioninDB + P3bonuss;
                // Parent 3 Leadership Bonus
                Boolean LshipCommition = p1.UpdateLcommition(Parent_3, final_P3_Lcom, Productpv, Productbv);

                #endregion

                #region Parent2

                ebns1 = bns.Getbonus(Parent_2);
                double T_commitioninDBP2 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P2bonus = ((mothbv * 0.5) / 100);
                Double final_P2_Lcom = T_commitioninDBP2 + P2bonus;
                // Parent 3 Leadership Bonus
                Boolean LshipCommitionP2 = p1.UpdateLcommition(Parent_2, final_P2_Lcom, Productpv, Productbv);

                #endregion

                #region Parent1

                ebns1 = bns.Getbonus(Parent_1);
                double T_commitioninDBP1 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P1bonus = ((mothbv * 1) / 100);
                Double final_P1_Lcom = T_commitioninDBP1 + P1bonus;
                // Parent 3 Leadership Bonus
                Boolean LshipCommitionP1 = p1.UpdateLcommition(Parent_1, final_P1_Lcom, Productpv, Productbv);

                #endregion

                #endregion

                Return_userID = Parent_3;
            }
            else if (Status.Equals("9") && parent1_status.Equals("9") && parent2_status.Equals("9"))
            {
                #region Child --> parent --> Parent 

                #region Parent2

                ebns1 = bns.Getbonus(Parent_2);
                double T_commitioninDBP2 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P2bonus = ((mothbv * 0.5) / 100);
                Double final_P2_Lcom = T_commitioninDBP2 + P2bonus;
                // Parent 3 Leadership Bonus
                Boolean LshipCommitionP2 = p1.UpdateLcommition(Parent_2, final_P2_Lcom, Productpv, Productbv);

                #endregion

                #region Parent1

                ebns1 = bns.Getbonus(Parent_1);
                double T_commitioninDBP1 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P1bonus = ((mothbv * 1) / 100);
                Double final_P1_Lcom = T_commitioninDBP1 + P1bonus;
                // Parent 3 Leadership Bonus
                Boolean LshipCommitionP1 = p1.UpdateLcommition(Parent_1, final_P1_Lcom, Productpv, Productbv);

                #endregion

                Return_userID = Parent_2;

                #endregion
            }
            else if (Status.Equals("9") && parent1_status.Equals("9"))
            {
                #region Child --> parent 

                #region Parent1

                ebns1 = bns.Getbonus(Parent_1);
                double T_commitioninDBP1 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P1bonus = ((mothbv * 1) / 100);
                Double final_P1_Lcom = T_commitioninDBP1 + P1bonus;
                // Parent 3 Leadership Bonus
                Boolean LshipCommitionP1 = p1.UpdateLcommition(Parent_1, final_P1_Lcom, Productpv, Productbv);

                #endregion

                Return_userID = Parent_1;

                #endregion
            }

            else if (Status.Equals("10") && parent1_status.Equals("10") && parent2_status.Equals("10") && parent3_status.Equals("10") && parent4_status.Equals("10") && parent5_status.Equals("10"))
            {
                #region Child --> parent --> Parent--> Parent--> Parent--> Parent--> Parent

                #region Parent5

                ebns1 = bns.Getbonus(Parent_5);
                double T_commitioninDB5 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P5bonuss = ((mothbv * 0.5) / 100);
                Double final_P5_Lcom = T_commitioninDB5 + P5bonuss;
                // Parent 3 Leadership Bonus
                Boolean LshipCommition5 = p1.UpdateLcommition(Parent_5, final_P5_Lcom, Productpv, Productbv);

                #endregion

                #region Parent4

                ebns1 = bns.Getbonus(Parent_4);
                double T_commitioninDB4 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P4bonuss = ((mothbv * 0.5) / 100);
                Double final_P4_Lcom = T_commitioninDB4 + P4bonuss;
                // Parent 3 Leadership Bonus
                Boolean LshipCommition4 = p1.UpdateLcommition(Parent_4, final_P4_Lcom, Productpv, Productbv);

                #endregion

                #region Parent3

                ebns1 = bns.Getbonus(Parent_3);
                double T_commitioninDB3 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P3bonuss = ((mothbv * 0.5) / 100);
                Double final_P3_Lcom = T_commitioninDB3 + P3bonuss;
                // Parent 3 Leadership Bonus
                Boolean LshipCommition3 = p1.UpdateLcommition(Parent_3, final_P3_Lcom, Productpv, Productbv);

                #endregion

                #region Parent2

                ebns1 = bns.Getbonus(Parent_2);
                double T_commitioninDBP2 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P2bonus = ((mothbv * 1) / 100);
                Double final_P2_Lcom = T_commitioninDBP2 + P2bonus;
                // Parent 3 Leadership Bonus
                Boolean LshipCommitionP2 = p1.UpdateLcommition(Parent_2, final_P2_Lcom, Productpv, Productbv);

                #endregion

                #region Parent1

                ebns1 = bns.Getbonus(Parent_1);
                double T_commitioninDBP1 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P1bonus = ((mothbv * 1) / 100);
                Double final_P1_Lcom = T_commitioninDBP1 + P1bonus;
                // Parent 3 Leadership Bonus
                Boolean LshipCommitionP1 = p1.UpdateLcommition(Parent_1, final_P1_Lcom, Productpv, Productbv);

                #endregion

                #endregion

                Return_userID = Parent_5;
 
            }
            else if (Status.Equals("10") && parent1_status.Equals("10") && parent2_status.Equals("10") && parent3_status.Equals("10") && parent4_status.Equals("10"))
            {
                #region Child --> parent --> Parent--> Parent--> Parent--> Parent


                #region Parent4

                ebns1 = bns.Getbonus(Parent_4);
                double T_commitioninDB4 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P4bonuss = ((mothbv * 0.5) / 100);
                Double final_P4_Lcom = T_commitioninDB4 + P4bonuss;
                // Parent 3 Leadership Bonus
                Boolean LshipCommition4 = p1.UpdateLcommition(Parent_4, final_P4_Lcom, Productpv, Productbv);

                #endregion

                #region Parent3

                ebns1 = bns.Getbonus(Parent_3);
                double T_commitioninDB3 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P3bonuss = ((mothbv * 0.5) / 100);
                Double final_P3_Lcom = T_commitioninDB3 + P3bonuss;
                // Parent 3 Leadership Bonus
                Boolean LshipCommition3 = p1.UpdateLcommition(Parent_3, final_P3_Lcom, Productpv, Productbv);

                #endregion

                #region Parent2

                ebns1 = bns.Getbonus(Parent_2);
                double T_commitioninDBP2 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P2bonus = ((mothbv * 1) / 100);
                Double final_P2_Lcom = T_commitioninDBP2 + P2bonus;
                // Parent 3 Leadership Bonus
                Boolean LshipCommitionP2 = p1.UpdateLcommition(Parent_2, final_P2_Lcom, Productpv, Productbv);

                #endregion

                #region Parent1

                ebns1 = bns.Getbonus(Parent_1);
                double T_commitioninDBP1 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P1bonus = ((mothbv * 1) / 100);
                Double final_P1_Lcom = T_commitioninDBP1 + P1bonus;
                // Parent 3 Leadership Bonus
                Boolean LshipCommitionP1 = p1.UpdateLcommition(Parent_1, final_P1_Lcom, Productpv, Productbv);

                #endregion

                #endregion

                Return_userID = Parent_4;

            }
            else if (Status.Equals("10") && parent1_status.Equals("10") && parent2_status.Equals("10") && parent3_status.Equals("10"))
            {
                #region Child --> parent --> Parent--> Parent

                #region Parent3

                ebns1 = bns.Getbonus(Parent_3);
                double T_commitioninDB3 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P3bonuss = ((mothbv * 0.5) / 100);
                Double final_P3_Lcom = T_commitioninDB3 + P3bonuss;
                // Parent 3 Leadership Bonus
                Boolean LshipCommition3 = p1.UpdateLcommition(Parent_3, final_P3_Lcom, Productpv, Productbv);

                #endregion

                #region Parent2

                ebns1 = bns.Getbonus(Parent_2);
                double T_commitioninDBP2 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P2bonus = ((mothbv * 1) / 100);
                Double final_P2_Lcom = T_commitioninDBP2 + P2bonus;
                // Parent 3 Leadership Bonus
                Boolean LshipCommitionP2 = p1.UpdateLcommition(Parent_2, final_P2_Lcom, Productpv, Productbv);

                #endregion

                #region Parent1

                ebns1 = bns.Getbonus(Parent_1);
                double T_commitioninDBP1 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P1bonus = ((mothbv * 1) / 100);
                Double final_P1_Lcom = T_commitioninDBP1 + P1bonus;
                // Parent 3 Leadership Bonus
                Boolean LshipCommitionP1 = p1.UpdateLcommition(Parent_1, final_P1_Lcom, Productpv, Productbv);

                #endregion

                Return_userID = Parent_3;

                #endregion

            }
            else if (Status.Equals("10") && parent1_status.Equals("10") && parent2_status.Equals("10"))
            {
                #region Child --> parent --> Parent

                #region Parent2

                ebns1 = bns.Getbonus(Parent_2);
                double T_commitioninDBP2 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P2bonus = ((mothbv * 1) / 100);
                Double final_P2_Lcom = T_commitioninDBP2 + P2bonus;
                // Parent 3 Leadership Bonus
                Boolean LshipCommitionP2 = p1.UpdateLcommition(Parent_2, final_P2_Lcom, Productpv, Productbv);

                #endregion

                #region Parent1

                ebns1 = bns.Getbonus(Parent_1);
                double T_commitioninDBP1 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P1bonus = ((mothbv * 1) / 100);
                Double final_P1_Lcom = T_commitioninDBP1 + P1bonus;
                // Parent 3 Leadership Bonus
                Boolean LshipCommitionP1 = p1.UpdateLcommition(Parent_1, final_P1_Lcom, Productpv, Productbv);

                #endregion

                #endregion

                Return_userID = Parent_2;

            }
            else if (Status.Equals("10") && parent1_status.Equals("10"))
            {
                #region Child --> parent

                #region Parent1

                ebns1 = bns.Getbonus(Parent_1);
                double T_commitioninDBP1 = double.Parse(ebns1.T_commision.ToString());
                // Get commission from DB+Add product Commission
                Double P1bonus = ((mothbv * 1) / 100);
                Double final_P1_Lcom = T_commitioninDBP1 + P1bonus;
                // Parent 3 Leadership Bonus
                Boolean LshipCommitionP1 = p1.UpdateLcommition(Parent_1, final_P1_Lcom, Productpv, Productbv);

                #endregion

                Return_userID = Parent_1;

                #endregion

            }

            return Return_userID;

        }




    }

}// namespace